<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item,index) in sysrolelist"
        :key="item.roleId"
        class="role-item"
        :class="{'active':findindex===index}"
        @click="classactive(index)"
      >
        <div class="role-info">
          <svg-icon :icon-class="findindex===index ? 'user-active' : 'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="$router.push(`/sys/addrole?id=${item.roleId}`)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="deleterole(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/sys/addrole')">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="permission">
          <div class="tree-wrapper">
            <div v-for="item in treerolelist" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }} </div>
              <el-tree
                ref="tree"
                :data="item.children"
                :props="defaultProps"
                :default-expand-all="true"
                show-checkbox
                node-key="id"
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员列表(${total})`" name="users">
          <div class="user-wrapper">
            <el-table
              :data="roleuserlist"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>

    </div>
  </div>
</template>

<script>
import { getsysroleAPI, gettreeroleAPI, getRoleDetailAPI, getRoleUserAPI, deleteroleAPI } from '@/api/sysrole'
export default {
  name: 'Role',
  data() {
    return {
      // 角色
      sysrolelist: [],
      // 角色权限列表
      treerolelist: [],
      // 生成树形结构
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      findindex: 0,
      activeName: 'permission',
      params: {
        page: 1,
        pageSize: 10
      },
      roleuserlist: [],
      total: null,
      perms: []
    }
  },
  async created() {
    await this.getsysrole()
    await this.gettreerole()
    this.classactive(0)
  },
  methods: {
    // 获取角色列表
    async getsysrole() {
      const res = await getsysroleAPI()
      // console.log(res.data)
      this.sysrolelist = res.data
    },
    // 获取角色权限列表
    async gettreerole() {
      const res = await gettreeroleAPI()
      // console.log(res)
      this.treerolelist = res.data
      this.addDisabled(this.treerolelist)
    },
    // 递归处理函数，展示禁用状态
    addDisabled(treerolelist) {
      // console.log(treerolelist)
      treerolelist.forEach(item => {
        item.disabled = true
        if (item.children) {
          this.addDisabled(item.children)
        }
      })
      // console.log(this.treerolelist)
    },
    // 点击菜单 、、改变样式拿到id继续执行下面方法
    classactive(index) {
      this.findindex = index
      // 得到ID
      const id = this.sysrolelist[index].roleId
      // 获取角色都是什么权限
      this.getRoleDetail(id)
      // 获取角色的权限成员列表
      this.getRoleUser(id)
    },
    // 获取角色都是什么权限
    async getRoleDetail(id) {
      const res = await getRoleDetailAPI(id)
      this.perms = res.data.perms
      this.$refs.tree.forEach((item, index) => {
        item.setCheckedKeys(this.perms[index])
      })
    },
    // 获取角色的权限成员列表
    async getRoleUser(id) {
      const res = await getRoleUserAPI(id, this.params)
      // console.log(res)
      this.total = res.data.total
      this.roleuserlist = res.data.rows
    },
    // 删除角色
    deleterole(id) {
      this.$confirm('你确定要删除此角色吗', '温馨提示').then(async() => {
        await deleteroleAPI(id)
        this.$message.success('删除角色成功')
        this.getsysrole()
      }).catch(() => {})
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding:20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
